package edu.seu.mart.service.impl;

import edu.seu.mart.dao.ICartDao;

import edu.seu.mart.entity.Pager;
import edu.seu.mart.model.Cart;
import edu.seu.mart.model.MartException;
import edu.seu.mart.service.ICartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * Created by Administrator on 2015/3/11.
 */
@Service
public class CartService implements ICartService {

    @Resource
    private ICartDao cartDao;

    @Override
    public void add(Cart cart) {
        cartDao.add(cart);
    }

    @Override
    public Pager<Cart> findUserCart(int userId) {
        return cartDao.findUserCart(userId);
    }

    @Override
    public void delete(int id) {
        cartDao.delete(id);
    }

    @Override
    public void deleteChecked(String[] ids) {
        try {
            for (String id : ids) {
                cartDao.delete(Integer.valueOf(id));
            }
        } catch (Exception e) {
            throw new MartException("选中的订单删除失败");
        }
    }

    @Override
    public void delete(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                cartDao.delete(id);
            }
        } catch (Exception e) {
            throw new MartException("选中的订单删除失败");
        }
    }

    /**
     * 将购物车选中商品按商家分成不同的订单
     */
    @Override
    public Map<Integer, List<Cart>> cartChecked(List<Integer> ids) {
        Map<String, Object> alias = new HashMap<>();
        alias.put("ids", ids);
        List<Cart> cartlist = cartDao.list("from Cart where id in (:ids)", alias);
        Map<Integer, List<Cart>> order = new HashMap<>();
        for (Cart cart : cartlist) {
            Integer sellerId = cart.getGoods().getSeller().getId();
            if (!order.containsKey(sellerId)) {
                List<Cart> list = new ArrayList<>();
                list.add(cart);
                order.put(sellerId, list);
            } else {
                order.get(sellerId).add(cart);
            }
        }
        return order;
    }

    @Override
    public void updateCart(int cartId, int num) {
        Cart cart = cartDao.load(cartId);
        if (cart.getQuantity() != num) {
            cart.setQuantity(num);
            cart.setSubtotal(cart.getGoods().getPrice() * num);
            cartDao.update(cart);
        }
    }

    @Override
    public List<Cart> userCartList(int userId) {
        return cartDao.list("from Cart where user_id = '"+ userId +"'");
    }

    @Override
    public int getCount(int userId) {
        return cartDao.getCount(userId);
    }

    @Override
    public Cart load(int id) {
        return cartDao.load(id);
    }
    @Override
    public Map<Integer, List<Cart>> cartChecked(Integer[] ids) {
        Map<String, Object> alias = new HashMap<>();
        alias.put("ids", Arrays.asList(ids));
        List<Cart> cartlist = cartDao.list("from Cart where id in (:ids)", alias);
        Map<Integer, List<Cart>> order = new HashMap<>();
        for (Cart cart : cartlist) {
            Integer sellerId = cart.getGoods().getSeller().getId();
            if (!order.containsKey(sellerId)) {
                List<Cart> list = new ArrayList<>();
                list.add(cart);
                order.put(sellerId, list);
            } else {
                order.get(sellerId).add(cart);
            }
        }
        return order;
    }
}
